package com.inspur.zhangpeixia.goods.servlet;
import com.inspur.zhangpeixia.goods.entity.User;
import com.inspur.zhangpeixia.goods.service.LoginService;
import com.inspur.zhangpeixia.goods.service.impl.LoginServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "loginServlet",value="/userlog")
public class LoginServlet extends HttpServlet {
    private LoginService loginService = new LoginServiceImpl();
//登录
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);
        String userAccount = req.getParameter("loginAccount");//表单name
        String userPassword = req.getParameter("password");
        User loginUser = loginService.login(userAccount,userPassword);

        if(loginUser!=null){
            req.getSession().setAttribute("loginUser",loginUser);
            resp.sendRedirect("index.jsp");
        }else{
            req.setAttribute("msg","用户名或密码错误");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
    }
//退出
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("loginUser");
        resp.sendRedirect("login.jsp");
    }
}
